package com.ti.ouxun.ui;

import com.ti.ouxun.ui.fragment.CompanyDescFragment;
import com.ti.ouxun.ui.fragment.DownloadFragment;
import com.ti.ouxun.ui.fragment.HomeFragment;
import com.ti.ouxun.ui.fragment.ProductFragment;
import com.ti.ouxun.util.KEY;
import com.ti.ouxun.util.SharedPreferencesUtil;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class FragmentTabHostActivity extends FragmentActivity {
	
	private FragmentTabHost mTabHost;
	private Button mBtnSpain;
	private Button mBtnEnglish;
	
	private Class<?> mTabSpecFragments[] = {
			HomeFragment.class,
			ProductFragment.class,
			CompanyDescFragment.class,
			DownloadFragment.class};
	
	private int[] mTabSpecBackgroundImgs = {
			R.drawable.selector_tab,
			R.drawable.selector_tab,
			R.drawable.selector_tab,
			R.drawable.selector_tab};
	
	private String mTabSpecTags[] = {
			"home", 
			"product", 
			"companydesc", 
			"download"};
	
	private int mTabSpecIcons[] = {
			R.drawable.ic_tab_home,
			R.drawable.ic_tab_product,
			R.drawable.ic_tab_companydesc,
			R.drawable.ic_tab_download};
	
	private String mTabSpecNames[] = {
			"Home",
			"Product",
			"Company\nDescription",
			"Offline\nDownload"};

	@Override
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		
		setContentView(R.layout.activity_main);
		findView();
		afterViews();
	}
	
	private void findView() {
		mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
		mBtnSpain = (Button) findViewById(R.id.btn_spain);
		mBtnEnglish = (Button) findViewById(R.id.btn_english);
	}
	
	private void afterViews() {
		initTabHost();
		
		// 若要一直存在tabHost, 那么不需要执行setListener() 方法
		setTabItemListener();
		
		// 设置语言监听
		setLanguageListener();
	}
	
	private void initTabHost() {
		// 设置TabHost
		mTabHost.setup(this, getSupportFragmentManager(), R.id.fragment_container);
		
		int count = getTabItemsCount();
		for(int i=0; i<count; i++) {
			// 为每一个Tab按钮设置图标，文字和内容
			TabSpec tabSpec = mTabHost.newTabSpec(mTabSpecTags[i]);
			tabSpec.setIndicator( getTabItemIndicator(i) );
			// 将Tab按钮添加进Tab选项中
			mTabHost.addTab(tabSpec, mTabSpecFragments[i], null);
		}
	}
	
	private void setTabItemListener() {
		int count = getTabItemsCount();
		// 为每一个tab设置点击事件
		for(int i=0; i<count; i++) {
			final int j = i;
			View tabItemView = mTabHost.getTabWidget().getChildAt(i);
			tabItemView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// 在用户点击底部的tab后，移除其他tab的fragment栈，并跳转到指定的tab
					setCurrentTabAndPopBackAllStack(j);
				}
			});
		}
	}
	
	private void setLanguageListener() {
		// 默认为英语
		mBtnEnglish.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SharedPreferencesUtil.putInt(FragmentTabHostActivity.this, KEY.SHARED_NAME, KEY.LANGUAGE_KEY, 0);
				setBtnLanguageBackground(0);
			}
		});
		
		mBtnSpain.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				SharedPreferencesUtil.putInt(FragmentTabHostActivity.this, KEY.SHARED_NAME, KEY.LANGUAGE_KEY, 1);
				setBtnLanguageBackground(1);
			}
		});
	}
	
	private void setBtnLanguageBackground(int select) {
		switch (select) {
		case 0:
			mBtnEnglish.setBackgroundResource(R.drawable.ic_language_selected);
			mBtnSpain.setBackgroundResource(R.drawable.selector_btn_language);
			break;
		case 1:
			mBtnSpain.setBackgroundResource(R.drawable.ic_language_selected);
			mBtnEnglish.setBackgroundResource(R.drawable.selector_btn_language);
			break;
		default:
			break;
		}
	}
	
	private int getTabItemsCount() {
		return mTabSpecFragments.length;
	}
	
	private View getTabItemIndicator(int index) {
		View tabItem = View.inflate(this, R.layout.item_tab_view, null);
		tabItem.setBackgroundResource(mTabSpecBackgroundImgs[index]);
		((ImageView)tabItem.findViewById(R.id.tab_icon)).setImageResource(mTabSpecIcons[index]);
		((TextView)tabItem.findViewById(R.id.tab_name)).setText(mTabSpecNames[index]);
		return tabItem;
	}

	private void setCurrentTabAndPopBackAllStack(int currentTabIndex) {
		popBackAllStack();
		mTabHost.setCurrentTab( currentTabIndex );
	}
	
	public void popBackAllStack() {
		getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	}
	
}
